package bubbleindex;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.Callable;

/**
 *
 * @author t
 */
public class UpdateRunnable implements Callable<Integer> {

    private final String Category;
    private final String Selections;
    private final String Sources;
    private final String quandlDataSet;
    private final String quandlDataName;
    private final int quandlColumn;
    private final String isYahooIndex;
    
    /**
     * 
     * @param Category
     * @param Selections
     * @param Sources
     * @param quandlDataSet
     * @param quandlDataName
     * @param quandlColumn
     * @param isYahooIndex 
     */
    public UpdateRunnable (final String Category, final String Selections, final String Sources,
            final String quandlDataSet, final String quandlDataName, final int quandlColumn,
            final String isYahooIndex) {
        
        this.Category = Category;
        this.Selections = Selections;
        this.Sources = Sources;
        this.quandlDataSet = quandlDataSet;
        this.quandlDataName = quandlDataName;
        this.quandlColumn = quandlColumn;
        this.isYahooIndex = isYahooIndex;
    }
    
    /**
     * 
     * @return 
     */
    @Override
    public Integer call() {
        if (!RunContext.Stop) {
            
                final URLS selection = new URLS();
                selection.setDataName(Selections);
                if (isYahooIndex.equals("1")) {
                    selection.isSpecial();
                }
                selection.setDataType(Category);
                selection.setSource(Sources);

                if (selection.getSource().equals("QUANDL")) {
                    selection.setQuandlUrl(quandlDataSet, quandlDataName);
                    selection.setQuandlColumn(quandlColumn);
                }

                else if (selection.getSource().equals("FED")) {
                    selection.setFEDUrl();
                }

                else {

                    selection.setYahooUrl();
                }

                final ByteArrayOutputStream outputstream = new ByteArrayOutputStream();

                if (!RunContext.Stop) {

                    try {
                        selection.readURL_file(outputstream);
                    } catch (final IOException ex) {
                        Logs.myLogger.error("Category = {}, Selections = {}. {}", Category, Selections, ex);
                        return 1;
                    } catch (final Throwable th) {
                        Logs.myLogger.error("Category = {}, Selections = {}. {}", Category, Selections, th);
                        return 1;
                    }
                        
                }
                
                if (!RunContext.Stop) {

                    if (outputstream.size() > 1000) {
                        try {
                            selection.cleanData(outputstream);
                        } catch (final IOException ex) {
                            Logs.myLogger.error("Category = {}, Selections = {}. {}", Category, Selections, ex);
                            return 1;
                        } catch (final Throwable th) {
                            Logs.myLogger.error("Category = {}, Selections = {}. {}", Category, Selections, th);
                            return 1;
                        }
                    }
                    else {
                        Logs.myLogger.error("Category = {}, Selections = {}. {}", Category, Selections, "Outputstream size = 0.");
                        return 1;
                    }
                                
                }
                
                return 0;

        }
        else {
            return 0;
        } 
    }
}
